package com.example.demo.pdf;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 * @author 张小张
 * @describe 压缩多个文件
 * @date 2023/07/18 18:10
 */
public class ZipUtils {
    public static void batchFileToZIP(Map<String, ByteArrayOutputStream> byteList, ByteArrayOutputStream byteOutPutStream) {
        ZipOutputStream zipOutputStream = new ZipOutputStream(byteOutPutStream);
        try {
            for (Map.Entry<String,ByteArrayOutputStream> entry : byteList.entrySet()) {
                //写入一个条目，我们需要给这个条目起个名字，相当于起一个文件名称
                zipOutputStream.putNextEntry(new ZipEntry(entry.getKey()));
                zipOutputStream.write(entry.getValue().toByteArray());
            }
            zipOutputStream.closeEntry();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                zipOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public static ByteArrayOutputStream downZipFile(Map<String,ByteArrayOutputStream> byteFileMap) {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        //自己的取流接口
        batchFileToZIP(byteFileMap, byteArrayOutputStream);
        return byteArrayOutputStream;
    }

}

